<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <!--<link rel="stylesheet" type="text/css" href="/easyui/themes/metro/easyui.css"/>-->
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/topjui/css/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/topjui/themes/default/topjui.bluelight.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link type="text/css" href="/topjui/css/myui.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/static/plugins/raty/jquery.raty.css">
    <script type="text/javascript" src="/static/public/js/topjui.config.js"></script>
    <!-- layui框架js -->
    <script src="/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
    <script src="/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/topjui/js/sysutils.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=lqVkw7DAWGt8CA9NAzxGWrQq"></script>
    <script src="/static/plugins/raty/jquery.raty.js"></script>
</head>

<body>

<table id="rowgrid" class="topjui-datagrid"></table>

<script type="text/javascript" charset="utf-8" th:inline="none">
    var listgrid;
    $(function(){
        listgrid = $('#rowgrid').datagrid({
            url:"/normal/list",
            method:'get',
            fit : true,
            fitColumns : true,
            border : false,
            pagination : true,
            idField : 'id',
            pageSize : 20,
            pageList : [ 20, 50, 100 ],
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'boardname',
                    title:'配电房名称',
                    width:60
                }, {
                    field:'voltmark',
                    title:'电压等级',
                    hidden:true
                }, {
                    field:'volume',
                    title:'电压等级',
                    hidden:true
                }, {
                    field:'usertype',
                    title:'用户类型',
                    hidden:true
                }, {
                    field:'factoryname',
                    title:'单位名称',
                    sortable:true,
                    width:60
                }, {
                    field:'factoryuser',
                    title:'单位联系人',
                    width:40
                }, {
                    field:'factoryphone',
                    title:'单位联系方式',
                    width:40
                }, {
                    field:'checktime',
                    title:'定时巡检日期',
                    hidden:true
                }, {
                    field:'projectid',
                    title:'所属项目ID',
                    hidden:true
                }, {
                    field:'projectcode',
                    title:'所属项目编码',
                    hidden:true
                }, {
                    field:'projectname',
                    title:'所属项目名称',
                    hidden:true
                }, {
                    field:'routerid',
                    title:'所属巡视单位ID',
                    hidden:true
                }, {
                    field:'routername',
                    title:'巡视单位',
                    width:60
                }, {
                    field:'cunsumername',
                    title:'巡视人员',
                    width:40
                }, {
                    field:'cunsumermobile',
                    title:'巡视手机',
                    width:40
                }, {
                    field:'boardlng',
                    title:'配电房所在位置lng',
                    hidden:true
                }, {
                    field:'boardlat',
                    title:'配电房所在位置lat',
                    hidden:true
                }, {
                    field:'createtime',
                    title:'计划时间',
                }, {
                    field:'tasktype',
                    title:'任务类型',
                    width:40,
                    formatter:function (value,row,index) {
                        if(value == '10'){
                            return "设备操作"
                        }else if(value == '20'){
                            return "保电"
                        }else if(value == '30'){
                            return "检修"
                        }else if(value == '40'){
                            return "设备清扫"
                        }else if(value == '50'){
                            return "巡视"
                        }else if(value == '60'){
                            return "红外测温"
                        }else if(value == '70'){
                            return "开关核对"
                        }else if(value == '80'){
                            return "环境清扫"
                        }else{
                            return "需求响应"
                        }
                    }
                }, {
                    field:'status',
                    title:'状态',
                    width:30,
                    sortable:true,
                    align:'center',
                    formatter : function(value, row, index) {
                        if(value == '0'){
                            return '<spane>未开始</span>';
                        }else{
                            return '<spane>未知状态</span>';
                        }
                    }
                }, {
                    field : 'action',
                    title : '操作',
                    width : 60,
                    align:'center',
                    formatter : function(value, row, index) {
                        return formatString('<img onclick="list_editMapFun(\'{0}\');" style="cursor:pointer" src="{1}"/>&nbsp;&nbsp;&nbsp;<img onclick="list_editFun(\'{2}\');" style="cursor:pointer" src="{3}"/>&nbsp;&nbsp;&nbsp;<img style="cursor:pointer" onclick="list_deleteFun(\'{4}\');" src="{5}"/>', row.id, '/static/admin/images/xq.png', row.id, '/static/admin/images/pencil.png',  row.id, '/static/admin/images/edit_remove.png');
                    }
                }
            ]],
            toolbar:[{
                text:'删除',
                iconCls:'fa fa-trash',
                btnCls: 'topjui-btn-normal',
                handler:function(){
                    list_removeFun();
                }
            },'-', {
                text:'查询',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    list_search();
                }
            },'-', {
                text:'刷新',
                iconCls : 'fa fa-refresh',
                btnCls: 'topjui-btn-green',
                handler : function() {
                    listgrid.datagrid('reload');
                }
            }],
            onDblClickRow :function(rowIndex,rowData){
                list_editMapFun(rowData.id);
            }
        });
    });


    function list_editFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        var i = listgrid.datagrid('getRowIndex', id);
        var r = listgrid.datagrid('getRows');
        var o = r[i];
        if(o.status < 20) {
            $('<div/>').dialog({
                href: "/normal/edit",
                width: 350,
                height: 340,
                modal: true,
                title: '编辑信息',
                buttons: [{
                    text: '保存',
                    iconCls: 'fa fa-save',
                    btnCls: 'topjui-btn-normal',
                    handler: function () {
                        var d = $(this).closest('.window-body');
                        $('#customerform').form('submit', {
                            url: "/normal/update",
                            success: function (result) {
                                try {
                                    var r = $.parseJSON(result);
                                    if (r.statusCode == '200') {
                                        listgrid.datagrid('load');
                                        d.dialog('destroy');
                                    }
                                    $.messager.show({
                                        title: r.title,
                                        msg: r.message
                                    });
                                } catch (e) {
                                    $.messager.alert('提示', result);
                                }
                            }
                        });
                    }
                }],
                onClose: function () {
                    $(this).dialog('destroy');
                },
                onLoad: function () {
                    var index = listgrid.datagrid('getRowIndex', id);
                    var rows = listgrid.datagrid('getRows');
                    var o = rows[index];
                    $('#customerform').form('load', o);
                    //手动添加change事件（重要）；
                    $('#routerid').change();
                    //运维类型
                    $.ajax({
                        type: 'get',
                        url: '/sys/groupsParam',
                        data: {
                            groups: '运维类型'
                        },
                        cache: false,
                        dataType: 'json',
                        success: function (response) {
                            if (response.code == '200') {
                                secondDatas = response.result;
                                //遍历回传的数据添加到二级select
                                $.each(secondDatas, function (key, secondData) {
                                    if (o.tasktype == secondData.value) {
                                        var option = '<option value="' + secondData.value + '" selected>' + secondData.title + '</option>';
                                        $("#tasktype").append(option);
                                    } else {
                                        var option = '<option value="' + secondData.value + '">' + secondData.title + '</option>';
                                        $("#tasktype").append(option);
                                    }
                                });
                                //二级select展示
                                $("#tasktype").fadeIn("slow");
                            }
                        },
                        error: function () {
                            alert("请求失败")
                        }
                    });
                }
            });
        }else{
            $.messager.show({
                title : "提示",
                msg : "任务已接单，不可以修改"
            });
        }
    }

    function list_starFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        var i = listgrid.datagrid('getRowIndex', id);
        var r = listgrid.datagrid('getRows');
        var o = r[i];
        if(o.status == 40) {
            $('<div/>').dialog({
                href: "/normal/star",
                width: 350,
                height: 260,
                modal: true,
                title: '任务评价',
                onClose: function () {
                    listgrid.datagrid('load');
                    $(this).dialog('destroy');
                },
                onLoad: function () {
                    var index = listgrid.datagrid('getRowIndex', id);
                    var rows = listgrid.datagrid('getRows');
                    var o = rows[index];
                    $('#customerform').form('load', o);
                }
            });
        }else{
            $.messager.show({
                title : "提示",
                msg : "任务未结单，不可以评价"
            });
        }
    }

    function list_editMapFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href : "/normal/map",
            width : 700,
            height : 610,
            modal : true,
            title : '任务详情',

            onClose : function() {
                $(this).dialog('destroy');
            },
            onLoad : function() {
                var index = listgrid.datagrid('getRowIndex', id);
                var rows = listgrid.datagrid('getRows');
                var o = rows[index];
                $('#customerform').form('load', o);
                //运维类型
                $.ajax({
                    type:'get',
                    url:'/sys/groupsParam',
                    data:{
                        groups:'运维类型'
                    },
                    cache:false,
                    dataType:'json',
                    success:function(response){
                        if(response.code == '200'){
                            secondDatas = response.result;
                            //遍历回传的数据添加到二级select
                            $.each(secondDatas, function(key, secondData) {
                                if(o.tasktype == secondData.value){
                                    var option = '<option value="'+secondData.value+'" selected>'+secondData.title+'</option>';
                                    $("#tasktype").append(option);
                                }else{
                                    var option = '<option value="'+secondData.value+'">'+secondData.title+'</option>';
                                    $("#tasktype").append(option);
                                }
                            });
                            //二级select展示
                            $("#tasktype").fadeIn("slow");
                        }
                    },
                    error:function(){
                        alert("请求失败")
                    }
                });
            }
        });
    }

    function list_search(){
        $('<div/>').dialog({
            href : '/normal/search',
            width : 390,
            height : 240,
            modal : true,
            title : '查询',
            buttons : [{
                text : '查询',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    var param = form2Json("search_form");
                    $('#search_form').form('submit', {
                        url : '/task/list',
                        onSubmit:function(){
                            return true;
                        },
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                listgrid.datagrid({
                                    url:'/task/list',
                                    pagination : true,
                                    idField : 'id',
                                    pageSize : 20,
                                    pageList : [ 20, 50, 100 ],
                                    sortName : 'id',
                                    sortOrder : 'desc',
                                    queryParams: param,　　//关键之处
                                    data:r
                                });
                                d.dialog('destroy');

                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            }],
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

    function list_removeFun() {
        var rows = listgrid.datagrid('getChecked');
        var ids = [];
        if (rows.length > 0) {
            $.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
                if (r) {
                    for ( var i = 0; i < rows.length; i++) {
                        ids.push(rows[i].id);
                    }
                    $.ajax({
                        url : '/normal/remove',
                        data : {
                            ids : ids.join(',')
                        },
                        dataType : 'json',
                        success : function(r) {
                            if (r.statusCode=='200') {
                                listgrid.datagrid('load');
                                listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                            }
                            $.messager.show({
                                title : r.title,
                                msg : r.message
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title : '提示',
                msg : '请勾选要删除的记录！'
            });
        }
    }

    function list_deleteFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        var i = listgrid.datagrid('getRowIndex', id);
        var r = listgrid.datagrid('getRows');
        var o = r[i];
        if(o.status < 20) {
            $.messager.confirm('确认', '您是否要删除当前选中的项目？', function (r) {
                if (r) {
                    $.ajax({
                        url: '/normal/delete',
                        data: {
                            ids: id
                        },
                        dataType: 'json',
                        success: function (r) {
                            if (r.statusCode == '200') {
                                listgrid.datagrid('load');
                                listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                            }
                            $.messager.show({
                                title: '提示',
                                msg: r.message
                            });
                        }
                    });
                }
            });
        }else{
            $.messager.show({
                title: '提示',
                msg: '任务已接单，不能被删除'
            });
        }
    }

</script>

</body>
</html>